Ext.ns("InRecordsAdminView");
InRecordsAdminView = Ext.extend(Ext.Panel, {
	searchPanel : null,
	gridPanel : null,
	store : null,
	topbar : null,
	layout : "form",
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		InRecordsAdminView.superclass.constructor.call(this, {
					id : "InRecordsAdminView",
					title : "已入库产品列表",
					iconCls : "menu-car_apply",
					items : [this.searchPanel, this.gridPanel]
				});
	},
	initUIComponents : function() {
		this.searchPanel = new Ext.FormPanel({
					id : "InRecordsAdminSearchForm",
					height : 40,
					region : "north",
					frame : false,
					border : false,
					layout : "hbox",
					layoutConfig : {
						padding : "5",
						align : "middle"
					},
					defaults : {
						xtype : "label",
						margins : {
							top : 0,
							right : 4,
							bottom : 4,
							left : 4
						}
					},
					items : [{
								text : "请输入查询条件:"
							}, {
								text : "客户信息"
							}, {
								xtype : "textfield",
								id : "customerinfo",
								name : "customerinfo"
							}, {
								text : "生产编号"
							}, {
								xtype : "textfield",
								id : 'productcode',
								name : "inRecords.productcode"
							}, {
								text : "订单编号"
							}, {
								xtype : "textfield",
								id : 'ordercode',
								name : "inRecords.ordercode"
							}, {
								xtype : "button",
								text : "查询",
								iconCls : "btn-search",
								handler : function() {
									var c = Ext
											.getCmp("InRecordsAdminSearchForm");
									var d = Ext.getCmp("InRecordsGrid");
									var s = d.getStore();
									if (c.getForm().isValid()) {
										s.setBaseParam('customerinfo', Ext
														.getCmp("customerinfo")
														.getValue());
										s.setBaseParam('inRecords.productcode',
												Ext.getCmp("productcode")
														.getValue());
										s.setBaseParam('inRecords.ordercode',
												Ext.getCmp("ordercode")
														.getValue());
										s.load();
									}
								}
							}]
				});
		this.store = new Ext.data.JsonStore({
					autoLoad : true,
					url : 'listInRecords.do?action=distinct',
					paramNames : {
						start : 'start',
						limit : 'rownum'
					},// 重命名分页参数以和action中对象对应
					root : 'list',
					totalProperty : 'totalCount',
					idProperty : 'id',
					remoteSort : true,
					fields : ['isck', 'id', 'productcode', 'ordercode', 'op',
							'cargoid', 'cargonum', 'intime', 'customerid',
							'customername', 'deleteflag', 'modifier',
							'modifyflag', 'remark', 'lastmodifytime']
				});
		this.store.setDefaultSort('id', 'desc');
		var b = new Ext.grid.CheckboxSelectionModel();
		var a = new Ext.grid.ColumnModel({
			columns : [b, {
						header : "状态",
						dataIndex : 'isck',
						width : 50,
						renderer : function(v) {
							return v == 0 ? '未出库' : '已出库';
						}
					}, {
						header : "生产编号",
						dataIndex : 'productcode',
						width : 100,
						sortable : true
					}, {
						header : "订单编号",
						dataIndex : 'ordercode',
						width : 100,
						sortable : true
					}, {
						header : "货品数量",
						dataIndex : 'cargonum',
						width : 30,
						align : 'left',
						sortable : true
					}, {
						header : "客户",
						dataIndex : 'customername',
						width : 150,
						align : 'left',
						sortable : true
					}, {
						header : "入库时间",
						dataIndex : 'intime',
						width : 100,
						align : 'left',
						sortable : true
					}, {
						header : "管理",
						dataIndex : "id",
						width : 50,
						sortable : false,
						renderer : function(g, f, d, j, e) {
							var i = d.data.productcode;
							var h = "";
							if (isGranted("app.InRecordsAction.delInRecords")
									&& d.data.isck == 0) {
								h = '<button title="删除" value=" " class="btn-del" onclick="InRecordsAdminView.remove(\''
										+ i + '\')">&nbsp;</button>';
							}
							if (isGranted("app.InRecordsAction.saveInRecords")
									&& d.data.isck == 0) {
								h += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="InRecordsAdminView.edit(\''
										+ i + '\')">&nbsp;</button>';
							}
							if (isGranted("printBar")) {
								h += '&nbsp;<button title="打印条码" value=" " class="btn-print" onclick="printBar(\''
										+ i + '\')">&nbsp;</button>';
							}
							return h;
						}
					}],
			defaults : {
				sortable : true,
				menuDisabled : false,
				width : 100
			}
		});
		this.topbar = new Ext.Toolbar({
					id : "InRecordsAdminFootBar",
					height : 30,
					bodyStyle : "text-align:left",
					items : []
				});
		if (isGranted("app.InRecordsAction.saveInRecords")) {
			this.topbar.add(new Ext.Button({
						iconCls : "btn-add",
						text : "添加成品包",
						handler : function() {
							new InRecordsForm().show();
						}
					}));
		}
		if (isGranted("app.InRecordsAction.delInRecords")) {
			this.topbar.add(new Ext.Button({
						iconCls : "btn-del",
						text : "删除成品包",
						handler : function() {
							var e = Ext.getCmp("InRecordsGrid");
							var c = e.getSelectionModel().getSelections();
							if (c.length == 0) {
								Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
								return;
							}
							var f = Array();
							for (var d = 0; d < c.length; d++) {
								f.push(c[d].data.productcode);
							}
							InRecordsAdminView.remove("'" + f.join() + "'");
						}
					}));
		}
		if (isGranted("printBar")) {
			this.topbar.add(new Ext.Button({
						iconCls : "btn-print",
						text : "打印包标签",
						handler : function() {
							var e = Ext.getCmp("InRecordsGrid");
							var c = e.getSelectionModel().getSelections();
							if (c.length == 0) {
								Ext.ux.Toast.msg("信息", "请选择要打印的记录！");
								return;
							}
							var f = Array();
							for (var d = 0; d < c.length; d++) {
								f.push(c[d].data.productcode);
							}
							printBar(f.join());
						}
					}));
		}
		this.gridPanel = new Ext.grid.GridPanel({
					id : "InRecordsGrid",
					tbar : this.topbar,
					region : "center",
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					autoHeight : true,
					cm : a,
					sm : b,
					viewConfig : {
						forceFit : true,
						enableRowBody : false,
						showPreview : false
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 15,
								store : this.store,
								displayInfo : true,
								displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
								emptyMsg : "当前没有记录"
							})
				});
		this.gridPanel.addListener("rowdblclick", function(d, c, f) {
					d.getSelectionModel().each(function(e) {
								if (isGranted("app.InRecordsAction.saveInRecords")) {
									InRecordsAdminView.edit(e.data.productcode);
								}
							});
				});
	}
});
InRecordsAdminView.remove = function(b) {
	var a = Ext.getCmp("InRecordsGrid");
	Ext.Msg.confirm("信息确认", "您确认要删除生产编号" + b + "的记录吗？", function(c) {
				if (c == "yes") {
					Ext.Ajax.request({
								url : _ctxPath + "delInRecords.do?op=ajax",
								params : {
									'ids' : b
								},
								method : "post",
								success : function(response, opts) {
									var res = Ext.decode(response.responseText);
									if (res.success) {
										Ext.ux.Toast.msg("信息提示", "成功删除所选记录！");
										a.getStore().reload({
													params : {
														start : 0,
														limit : 25
													}
												});
									} else {
										Ext.Msg.alert("信息提示", res.message);
									}
								}
							});
				}
			});
};
InRecordsAdminView.edit = function(a) {
	new InRecordsForm({
				productid : a
			}).show();
};
var printBar = function(productcode) {
	var win = new Ext.Window({
		width : 800,
		height : 400,
		modal : true,
		title : '打印货品包条码',
		items : [new Ext.Panel({
			height : 500,
			html : "<iframe src='ReportServer?reportlet=printbar.cpt&productcode="
					+ productcode
					+ "' style='width:100%;height:100%' frameborder='0' marginwidth='0' marginheight='0'></iframe>"
		})]
	});
	win.show();
}